<?php
/**
 * 处理控制器
 */
namespace app\manage\controller\order;
use app\manage\controller\Base;
use app\common\query\Plugin;

class Handle extends Base
{
	private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    /**
     * [isfh 发货]
     * @return [type] [description]
     */
    public function deliver(){
        //商品ID 物流ID
        $param = $this->params;
        if(!$param['order_id'])return json(['code'=>0,'msg'=>'参数错误']);
        $data = model('Order')->getOneRow(['order_id'=>$param['order_id'],'status'=>1]);//订单是否存在，并且能执行发货
        if($data){
            //处理发货
            model('Order')->updateOrder($where,['status'=>2]);
            model('OrderDetails')->add(['logis_id'=>$param['logis_id'],'oid'=>$param['order_id'],'create_time'=>time(),'logis_order_id'=>$param['logis_no']]);//添加记录
            return json(['code'=>1,'msg'=>'发货成功']);
        }
        return json(['code'=>0,'msg'=>'此订单目前无法执行此操作']);
    }

    /**
     * [rejected 驳回]
     * @return [type] [description]
     */
    public function reject(){
        $param = $this->params;
        if(!$param['data'])return json(['code'=>0,'msg'=>'参数错误']);
        $where = self::setMark($param['data']);
        $data = model('Order')->getAllRow($where);
        if(!empty($data)){
            $res = model('Order')->updateOrder($where,['status'=>0]);
            if($res){
                foreach ($data as $k => $v){
                    self::orderRejectCoin($v['uid'],7,$v['credit'],$v['order_id']);
                    $row = model('OrderDetails')->getDatas(['order_id'=>$v['order_id']]);
                    foreach ($row as $key => $val){
                        self::orderRejectCoin($v['uid'],$val['coin_id'],$val['amount'],$v['order_id']);//货币积分返回
                        self::orderRejectGoods($val['gid'],$val['number']);//商品数量返回
                    }
                }
            }
            return json(['code'=>1,'msg'=>'驳回成功']);
        }
        return json(['code'=>0,'msg'=>'此订单目前无法执行此操作']);
    }

    /**
     * [setMark 搜索条件]
     * @param array $param [description]
     */
    private function setMark($param=[]){
        if(is_array($param)){
            $id = implode(',',$param);
            $where['order_id'] = ['in',$id];
            $where['status'] = '1';
        }else{
            $where['order_id'] = $param;
            $where['status'] = '1';
        }
        return $where;
    }

    /**
     * 用户订单驳回,返回金币
     */
    private function orderRejectCoin($uid='',$coin_id='',$amount='',$order_id=''){
          $msg = '管理员'.UID.'驳回订单'.$order_id.'返回货币';
          return model('Order')->orderMemberCoinRecord('inc',$uid,$coin_id,$amount,$msg);
    }

    /**
     * 用户订单驳回,商品数量增加
     */
    private function orderRejectGoods($gid='',$number=''){
          return model('MallGoods')->incTotal(['id'=>$gid],$number);
    }

}